import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class LotteryChecker {

    public static int checkPrizeLevel(int[] userRedBalls, int userBlueBall,
                                      int[] drawRedBalls, int drawBlueBall) {
        if (userRedBalls == null || drawRedBalls == null ||
                userRedBalls.length != 6 || drawRedBalls.length != 6) {
            throw new IllegalArgumentException("红球号码数组必须包含6个号码");
        }

        int redMatchCount = countRedMatches(userRedBalls, drawRedBalls);
        boolean blueMatch = (userBlueBall == drawBlueBall);

        return determinePrizeLevel(redMatchCount, blueMatch);
    }

    private static int countRedMatches(int[] userRedBalls, int[] drawRedBalls) {
        Set<Integer> drawRedSet = new HashSet<>();
        for (int ball : drawRedBalls) {
            drawRedSet.add(ball);
        }

        int matchCount = 0;
        for (int ball : userRedBalls) {
            if (drawRedSet.contains(ball)) {
                matchCount++;
            }
        }
        return matchCount;
    }

    private static int determinePrizeLevel(int redMatchCount, boolean blueMatch) {
        if (redMatchCount == 6 && blueMatch) {
            return 1;
        } else if (redMatchCount == 6) {
            return 2;
        } else if (redMatchCount == 5 && blueMatch) {
            return 3;
        } else if (redMatchCount == 5 || (redMatchCount == 4 && blueMatch)) {
            return 4;
        } else if (redMatchCount == 4 || (redMatchCount == 3 && blueMatch)) {
            return 5;
        } else if (blueMatch) {
            return 6;
        } else {
            return 0;
        }
    }

    public static String getPrizeName(int prizeLevel) {
        switch (prizeLevel) {
            case 1: return "一等奖";
            case 2: return "二等奖";
            case 3: return "三等奖";
            case 4: return "四等奖";
            case 5: return "五等奖";
            case 6: return "六等奖";
            default: return "未中奖";
        }
    }

    public static Object[] generateDrawNumbers() {
        int[] redBalls = generateUniqueRandomNumbers(1, 33, 6);
        int blueBall = generateRandomNumber(1, 16);
        Arrays.sort(redBalls);
        return new Object[]{redBalls, blueBall};
    }

    private static int[] generateUniqueRandomNumbers(int min, int max, int count) {
        Set<Integer> numberSet = new HashSet<>();
        while (numberSet.size() < count) {
            int randomNum = generateRandomNumber(min, max);
            numberSet.add(randomNum);
        }

        int[] result = new int[count];
        int index = 0;
        for (int num : numberSet) {
            result[index++] = num;
        }
        return result;
    }

    private static int generateRandomNumber(int min, int max) {
        return (int) (Math.random() * (max - min + 1)) + min;
    }

    public static boolean validateRedBalls(int[] redBalls) {
        if (redBalls.length != 6) {
            return false;
        }

        Set<Integer> ballSet = new HashSet<>();
        for (int ball : redBalls) {
            if (ball < 1 || ball > 33 || !ballSet.add(ball)) {
                return false;
            }
        }
        return true;
    }

    public static boolean validateBlueBall(int blueBall) {
        return blueBall >= 1 && blueBall <= 16;
    }

    public static String formatNumber(int number) {
        return String.format("%02d", number);
    }
}
